package cc.rengu.igas.channel.upqc.biz.impl;

import cc.rengu.igas.channel.upqc.common.constant.UpqcServiceConstant;
import cc.rengu.igas.channel.upqc.common.enums.RespCodeEnum;
import cc.rengu.igas.channel.upqc.facade.UpqcPayerService;
import cc.rengu.igas.channel.upqc.facade.request.*;
import cc.rengu.igas.channel.upqc.facade.response.*;
import cc.rengu.igas.channel.upqc.facade.result.Result;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.DubboResult;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;

public class UpqcPayerServiceImpl implements UpqcPayerService {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());

    @Override
    public Result<GenC2BNoResponse> genC2BNo(GenC2BNoRequest genC2BNoRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<GenC2BNoResponse> result = new Result<>();
        GenC2BNoResponse genC2BNoResponse = new GenC2BNoResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(genC2BNoRequest,
                    UpqcServiceConstant.GEN_C2BNO_SERVICE, GenC2BNoResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((GenC2BNoResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("c2b码申请失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                genC2BNoResponse.setRespDesc(((BizException) e).getCode());
                genC2BNoResponse.setRespDesc(e.getMessage());
            } else {
                genC2BNoResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                genC2BNoResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<C2BAddDealNoticeResponse> c2bAddDealNotice(C2BAddDealNoticeRequest c2bAddDealNoticeRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<C2BAddDealNoticeResponse> result = new Result<>();
        C2BAddDealNoticeResponse c2bAddDealNoticeResponse = new C2BAddDealNoticeResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(c2bAddDealNoticeRequest,
                    UpqcServiceConstant.C2B_ADD_NOTICE_SERVICE, C2BAddDealNoticeResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((C2BAddDealNoticeResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("c2b附加信息处理:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                c2bAddDealNoticeResponse.setRespDesc(((BizException) e).getCode());
                c2bAddDealNoticeResponse.setRespDesc(e.getMessage());
            } else {
                c2bAddDealNoticeResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                c2bAddDealNoticeResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<B2CQueryOrderResponse> b2cQueryOrder(B2CQueryOrderRequest b2CQueryOrderRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<B2CQueryOrderResponse> result = new Result<>();
        B2CQueryOrderResponse b2CQueryOrderResponse = new B2CQueryOrderResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(b2CQueryOrderRequest,
                    UpqcServiceConstant.B2C_QUERY_ORDER_SERVICE, B2CQueryOrderResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((B2CQueryOrderResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("查询订单失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                b2CQueryOrderResponse.setRespDesc(((BizException) e).getCode());
                b2CQueryOrderResponse.setRespDesc(e.getMessage());
            } else {
                b2CQueryOrderResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                b2CQueryOrderResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<B2CQueryCouponResponse> b2cQueryCoupon(B2CQueryCouponRequest b2CQueryCouponRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<B2CQueryCouponResponse> result = new Result<>();
        B2CQueryCouponResponse b2CQueryCouponResponse = new B2CQueryCouponResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(b2CQueryCouponRequest,
                    UpqcServiceConstant.B2C_QUERY_COUPON_SERVICE, B2CQueryCouponResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((B2CQueryCouponResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("查询优惠失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                b2CQueryCouponResponse.setRespDesc(((BizException) e).getCode());
                b2CQueryCouponResponse.setRespDesc(e.getMessage());
            } else {
                b2CQueryCouponResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                b2CQueryCouponResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<B2COrderPayResponse> b2cOrderPay(B2COrderPayRequest b2COrderPayRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<B2COrderPayResponse> result = new Result<>();
        B2COrderPayResponse b2COrderPayResponse = new B2COrderPayResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(b2COrderPayRequest,
                    UpqcServiceConstant.B2C_ORDER_PAY_SERVICE, B2COrderPayResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((B2COrderPayResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("付款失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                b2COrderPayResponse.setRespDesc(((BizException) e).getCode());
                b2COrderPayResponse.setRespDesc(e.getMessage());
            } else {
                b2COrderPayResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                b2COrderPayResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<B2COrderPayQueryResponse> b2cOrderPayQuery(B2COrderPayQueryRequest b2COrderPayQueryRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<B2COrderPayQueryResponse> result = new Result<>();
        B2COrderPayQueryResponse b2COrderPayQueryResponse = new B2COrderPayQueryResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(b2COrderPayQueryRequest,
                    UpqcServiceConstant.B2C_ORDER_PAY_QUERY_SERVICE, B2COrderPayQueryResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((B2COrderPayQueryResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("付款查询失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                b2COrderPayQueryResponse.setRespDesc(((BizException) e).getCode());
                b2COrderPayQueryResponse.setRespDesc(e.getMessage());
            } else {
                b2COrderPayQueryResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                b2COrderPayQueryResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<B2CATMCashNoticeResponse> b2cATMCashNotice(B2CATMCashNoticeRequest b2CATMCashNoticeRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<B2CATMCashNoticeResponse> result = new Result<>();
        B2CATMCashNoticeResponse b2CATMCashNoticeResponse = new B2CATMCashNoticeResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(b2CATMCashNoticeRequest,
                    UpqcServiceConstant.B2C_ATM_CASH_NOTICE_SERVICE, B2CATMCashNoticeResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((B2CATMCashNoticeResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("取现操作通知失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                b2CATMCashNoticeResponse.setRespDesc(((BizException) e).getCode());
                b2CATMCashNoticeResponse.setRespDesc(e.getMessage());
            } else {
                b2CATMCashNoticeResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                b2CATMCashNoticeResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }
}
